<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>Title</title>
</head>
<body>
<script>
  const organization = this.pageObj.getApiWidgetByAliasName('tree_organization');
  const three = this.pageObj.getApiWidgetByAliasName('三维组件');
  const [ select ] = $event;
  const value = select.value;

  console.log($event);

  const artemis = three.widgetObj.artemis;
  const dataSource = organization.widgetObj._option.dataSource.map(breakDataSource);

  if (value === 'all') {
    organization.widgetObj._option.dataSource = artemis.resetUsersStatus(dataSource);
  } else {
    organization.widgetObj._option.dataSource = artemis.filterUsersOnline(dataSource);
  }

  organization.widgetObj.render();
  console.log(organization.widgetObj._option.dataSource)
  function breakDataSource(item) {
    if(item.pointId || item.communicationName || item.personName) {
      item.id = item.personId;
      item.userId = item.account || item.communicationAccount;
      item.username = item.communicationName || item.personName;
      item.title = (item.communicationName || item.personName) + '(' + item.userId + ')';
      item.disabled = true
    } else if(item.orgId || item.orgName) {
      item.title = item.orgName;
      item.id = item.orgId;
    } else if (item.deptId || item.deptName) {
      item.title = item.deptName;
      item.id = item.deptId;
    } else if (item.distributionGroupId || item.distributionGroupName) {
      item.title = item.distributionGroupName;
      item.id = item.distributionGroupId;
    } else if (item.equipmentTypeName || item.equipmentTypeCode) {
      item.title = item.equipmentTypeName;
      item.id = item.equipmentTypeCode;
    }

    if (item.orgList) item.children = item.orgList.map(breakDataSource)
    if (item.groupList) item.children = item.groupList.map(breakDataSource)
    if (item.personList) item.children = item.personList.map(breakDataSource)
    if (item.accountData) item.children = item.accountData.map(breakDataSource)
    if (item.equipmentType) item.children = item.equipmentType.map(breakDataSource)

    return item
  }

  fetch('http://192.168.1.100:27003/pubCommunicationAccount/getOrgListByName?name=值班').then(res => res.json()).then(res => {
    const data = res.result.map(breakDataSource)
    console.log(res, data)
  })
</script>

<script>
  const organization = this.pageObj.getApiWidgetByAliasName('tree_organization');
  const search_input = this.pageObj.getApiWidgetByAliasName('search_input');


  function breakDataSource(item) {
    if(item.pointId || item.communicationName || item.personName) {
      item.id = item.personId;
      item.userId = item.account || item.communicationAccount;
      item.username = item.communicationName || item.personName;
      item.title = (item.communicationName || item.personName) + '(' + item.userId + ')';
      item.disabled = true
    } else if(item.orgId || item.orgName) {
      item.title = item.orgName;
      item.id = item.orgId;
    } else if (item.deptId || item.deptName) {
      item.title = item.deptName;
      item.id = item.deptId;
    } else if (item.distributionGroupId || item.distributionGroupName) {
      item.title = item.distributionGroupName;
      item.id = item.distributionGroupId;
    }

    if (item.orgList) item.children = item.orgList.map(breakDataSource)
    if (item.groupList) item.children = item.groupList.map(breakDataSource)
    if (item.personList) item.children = item.personList.map(breakDataSource)
    if (item.accountData) item.children = item.accountData.map(breakDataSource)

    return item
  }


  const text = $(search_input.__box).find('.input-filter').val();
  fetch(`/Communication/pubCommunicationAccount/getOrgListByName?name=${text || ''}`).then(res => res.json()).then(res => {
    const data = res.result.map(breakDataSource)
    organization.widgetObj._option = {
      ...organization.widgetObj._option,
      dataSource: data,
    }
    organization.widgetObj.render();
    console.log(res, data)
  })
</script>
</body>
</html>